Jetson Nano学习笔记

前言

JetsonNano是Nvidia的一个嵌入式开发板,具备Maxwell128核心的GPU和4核心 ARM A57的CPU,可运行Ubuntu(Linux for Tegra,L4T),浮点运算能力为472GFLOPS(FP16),官方给出的功率为10W。
JetsonNano可以用来搞些机器学习相关内容。

  1. JetsonNano平台只支持Python3.6的TensorFlow。
  2. JetsonNano镜像版本:JetPack4.4
  3. TensroFlow API版本:r2.1.0

以上信息为当前时间更新。

关于Jetson Nano Developer Kit

硬件 配置
GPU NVIDIA Maxwell™ 架构,配备 128 个 NVIDIA CUDA® 核心
CPU 四核 ARM® Cortex®-A57 MPCore 处理器
内存 4 GB 64 位 LPDDR4
存储 Micro SD 卡卡槽(需要另购16G以上SD卡接入)

image

  1. Micro SD 卡卡槽: 可接入TF卡(16G以上),烧写系统镜像
  2. 40PIN GPIO扩展接口(兼容树莓派40PIN接口)
  3. Micro USB接口:用于5V电源输入或者USB数据传输
  4. 千兆以太网口: 10/100/1000Base-T 自适应以太网端口
  5. USB3.0接口:4个USB3.0接口
  6. HDMI高清接口:用于外接HDMI屏幕
  7. DisplayPort接口:用于外接DP屏幕
  8. DC电源接口:用于外接5V电源(外径5.5, 内径2.1)
  9. MIPS CSI 摄像头接口:兼容树莓派摄像头接口

烧写镜像

官方教程

推荐使用Etcher烧写

镜像很大,有14G,烧写很慢,但安装系统很快。

系统版本为 Ubuntu 18.04.4 LTS (GNU/Linux 4.9.140-tegra aarch64)

安装tensorflow

安装方法为官方给出

link

安装版本为 Python 3.6 + JetPack4.4

1
2
3
4
5
6
7
$ sudo apt-get update

$ sudo apt-get install libhdf5-serial-dev hdf5-tools libhdf5-dev zlib1g-dev zip libjpeg8-dev liblapack-dev libblas-dev gfortran

$ sudo apt-get install python3-pip

$ sudo pip3 install -U pip

安装h5py会出错,尝试重启单独安装,这个有点玄学,我重刷了两次,单独安装才安装成功

1
$ sudo pip3 install -U pip testresources setuptools numpy==1.16.1 future==0.17.1 mock==3.0.5 h5py==2.9.0 keras_preprocessing==1.0.5 keras_applications==1.0.8 gast==0.2.2 futures protobuf pybind11

接下来是漫长的等待

1
2
3
4
5
# TF-2.x
$ sudo pip3 install --pre --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v44 tensorflow==2.2.0+nv20.6

# TF-1.15
$ sudo pip3 install --pre --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v44 ‘tensorflow<2
1
2
3
4
5
6
7
8
Successfully installed absl-py-0.9.0 astunparse-1.6.3 cachetools-4.1.1 gast-0.3.3 google-auth-1.19.1 google-auth-oauthlib-0.4.1 google-pasta-0.2.0 grpcio-1.30.0 h5py-2.10.0 importlib-metadata-1.7.0 keras-preprocessing-1.1.2 markdown-3.2.2 oauthlib-3.1.0 opt-einsum-3.2.1 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.24.0 requests-oauthlib-1.3.0 rsa-4.6 scipy-1.4.1 six-1.15.0 tensorboard-2.2.2 tensorboard-plugin-wit-1.7.0 tensorflow-2.2.0+nv20.6 tensorflow-estimator-2.2.0 termcolor-1.1.0 werkzeug-1.0.1 wrapt-1.12.1 zipp-3.1.0

nano@jetson:~$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
2020-07-16 08:53:15.536130: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.

安装jupyter

能否安装成功也有一定的玄学,遇到的问题有,pyzmq编译出错,远程登录无法识别密码。

安装方法同样来自官方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
安装nodejs
$ sudo apt install nodejs npm

安装jupyter
$ sudo pip3 install jupyter jupyterlab

$ sudo jupyter labextension install @jupyter-widgets/jupyterlab-manager

$ sudo jupyter labextension install @jupyterlab/statusbar

生成jupyter配置文件
$ jupyter lab --generate-config

$ jupyter notebook password
输入登陆密码
生成jupyter_notebook_config.json文件

修改jupyter_notebook_config.py的配置,添加以下内容

1
2
3
4
c.NotebookApp.ip = '*' # 就是设置所有ip皆可访问
c.NotebookApp.password = u'sha1:0fb67bb71f8f:9525f730807d01c04ea963492b0e3340de7b9d67' #jupyter_notebook_config.json里的sha1密文
c.NotebookApp.open_browser = False # 禁止自动打开浏览器
c.NotebookApp.port = 8888 #指定为NAT端口映射的端口号

启动

1
$ jupyter-lab

安装openCV

自带opencv,存在一个bug,如果先导入tensorflow会出错,但先导入cv2,再导入tensorflow则没有错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> import tensorflow
2020-07-16 08:53:15.536130: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.2
>>>
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/dist-packages/cv2/__init__.py", line 89, in <module>
bootstrap()
File "/usr/lib/python3.6/dist-packages/cv2/__init__.py", line 79, in bootstrap
import cv2
ImportError: /usr/lib/aarch64-linux-gnu/libgomp.so.1: cannot allocate memory in static TLS block

先导入cv2则不会出错

>>> import cv2
>>> import tensorflow
2020-07-16 08:56:07.842807: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.2

>>>

测试Tensorflow

首先导入tensorflow

1
import tensorflow as tf

看下版本

1
tf.__version__

'2.2.0'

载入并准备好 MNIST数据集。将样本从整数转换为浮点数:

1
2
3
4
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 1s 0us/step

用tf.keras.Sequential构建一个模型。
为模型选择优化器和损失函数:

1
2
3
4
5
6
7
8
9
10
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

开始训练

1
model.fit(x_train, y_train, epochs=5)
Epoch 1/5
1875/1875 [==============================] - 26s 14ms/step - loss: 0.2933 - accuracy: 0.9154
Epoch 2/5
1875/1875 [==============================] - 26s 14ms/step - loss: 0.1426 - accuracy: 0.9577
Epoch 3/5
1875/1875 [==============================] - 26s 14ms/step - loss: 0.1062 - accuracy: 0.9676
Epoch 4/5
1875/1875 [==============================] - 26s 14ms/step - loss: 0.0870 - accuracy: 0.9731
Epoch 5/5
1875/1875 [==============================] - 25s 14ms/step - loss: 0.0734 - accuracy: 0.9771

<tensorflow.python.keras.callbacks.History at 0x7f4c402710>

速度不是太慢,最后看下准确度

1
model.evaluate(x_test,  y_test, verbose=2)
313/313 - 2s - loss: 0.0704 - accuracy: 0.9803
[0.07044795155525208, 0.9803000092506409]

98%的准确度 jetson nano 完美运行tensorflow2

我想通过ssh远程运行图形程序,如何尝试都不行,直到在NVIDIA Metropolis Documentation发现了这句话。

ReleaseNotes 2.0 LIMITATIONS

1
2
On Jetson, running a DeepStream application over SSH (via putty) with X11 forwarding
does not work

总结

这个东西还是存在着许多bug,系统也不是非常稳定,开发也很麻烦,如果是想玩玩还是推荐树莓派。

0%